<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PunchCardRecord;
use App\Models\PunchCardUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class PunchCardRecordController extends AdminController
{
    public $title = '登记记录';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PunchCardRecord(['users']), function (Grid $grid) {
            $grid->id->sortable();
            $grid->day;
            $grid->openid;
            $grid->column('users.nickname', '微信昵称');
            $grid->column('users.avatar', '微信头像')->image('', 50, 50);

            $grid->status->display(function ($status) {
                return $status ? '主动登记' : '后台登记';
            })->label();
            $grid->remark->editable();
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('openid');
                $filter->where('nickname', function ($query) {

                    $query->whereHas('users', function ($query) {
                        $query->where('nickname', 'like', "%{$this->input}%");
                    });
                }, '微信昵称');
            });

            $grid->model()->orderBy('id', 'desc');
            $grid->export();
            // 回收站数据
            //  $grid->model()->onlyTrashed();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PunchCardRecord(), function (Show $show) {
            $show->id;
            $show->day;
            $show->openid;
            $show->status;
            $show->remark;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PunchCardRecord(), function (Form $form) {
            $form->display('id');
            $form->date('day')->format('YYYY-MM-DD');
            $form->text('openid');
            $form->hidden('status')->default(0);
            $form->text('remark');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $userInfo = PunchCardUser::where('openid', $form->input('openid'))->first();
                    if (empty($userInfo)) {
                        return $form->error('openid 不存在');
                    }

                    if ($userInfo->punch_num < 1) {
                        return $form->error('用户打卡次数小于 1');
                    }
                }
            });

            $form->saved(function (Form $form, $result) {
             
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    // 自增ID
                    // $newId = $result;
                    // // 也可以这样获取自增ID
                    $res = PunchCardUser::where('openid', $form->openid)->where('punch_num', '>', 0)->decrement('punch_num');
                    if (!$res) {
                        $form->destroy($result);
                        return $form->error('打卡次数减少失败');
                    }
                    return;
                }

                // 修改操作
            });
        });
    }
}
